import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Layout from "../views/home/Admin.vue"
import {markRaw} from "vue";

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'dashboard',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                name: 'dashboard',
                component: ()=>import("../views/dashboard/dashboard.vue"),
                meta: {
                    title: '仪表盘',
                    affix: true,
                    icon: 'Odometer',
                },
            }
        ]
    },
    {
        path: '/login',
        name: 'login',
        component: ()=>import("../views/user/Login.vue")
    },
    {
        path: '/editBlog',
        name: 'editBlog',
        component: Layout,
        redirect: '/editBlog/index',
        children: [
            {
                path: 'index',
                name: 'editBlog',
                component: ()=>import("../views/blog/editBlog.vue"),
                meta: {
                    title: '写文章',
                    affix: true,
                    icon: 'Edit',
                },
            }
        ]
    },
    {
        path: '/modules',
        component: Layout,
        name: 'modules',
        redirect: "/modules/blog",
        meta: {
            title: '管理模块',
            icon: 'Menu',
            roles: ['admin',]
        },
        children: [
            {
                path: 'blog',
                name: 'blog',
                component: ()=>import("../views/blog/blog.vue"),
                meta: {
                    title: '博客管理',
                    icon: 'Memo',
                }
            },
            {
                path: 'comment',
                name: 'comment',
                component: ()=>import("../views/comment/comment.vue"),
                meta: {
                    title: '评论管理',
                    icon: 'ChatRound',
                }
            },
            {
                path: 'category',
                name: 'category',
                component: ()=>import("../views/category/category.vue"),
                meta: {
                    title: '分类管理',
                    icon: 'CollectionTag',
                }
            },
            {
                path: 'tag',
                name: 'tag',
                component: ()=>import("../views/tag/tag.vue"),
                meta: {
                    title: '标签管理',
                    icon: 'PriceTag',
                }
            },
            {
                path: 'otherLink',
                name: 'otherLink',
                component: ()=>import("../views/system/otherLink.vue"),
                meta: {
                    title: '友情链接',
                    icon: 'Connection',
                }
            },
        ]
    },
    {
        path: '/log',
        name: 'log',
        component: Layout,
        redirect: '/log/operationLog',
        meta: {
            title: '日志管理',
            icon: 'Document',
            roles: ['admin',]
        },
        children: [
            {
                path: 'operationLog',
                name: 'operationLog',
                component: ()=>import("../views/log/operationLog.vue"),
                meta: {
                    title: '操作日志',
                    icon: 'MagicStick',
                }
            },
            {
                path: 'visitLog',
                name: 'visitLog',
                component: ()=>import("../views/log/visitLog.vue"),
                meta: {
                    title: '访问日志',
                    icon: 'Histogram',
                }
            },
            {
                path: 'exceptionLog',
                name: 'exceptionLog',
                component: ()=>import("../views/log/exceptionLog.vue"),
                meta: {
                    title: '异常日志',
                    icon: 'WarnTriangleFilled',
                }
            },
        ]
    },
    {
        path: '/system',
        name: 'system',
        component: Layout,
        redirect: '/system/systemConfig',
        meta: {
            title: '系统管理',
            icon: 'Setting',
            roles: ['admin',]
        },
        children: [
            {
                path: 'systemConfig',
                name: 'systemConfig',
                component: ()=>import("../views/system/systemConfig.vue"),
                meta: {
                    title: '系统配置',
                    icon: 'EditPen',
                }
            },
            {
                path: 'editUser',
                name: 'editUser',
                component: ()=>import("../views/system/editUser.vue"),
                meta: {
                    title: '修改用户',
                    icon: 'Lock',
                }
            },
            {
                path: 'editPassword',
                name: 'editPassword',
                component: ()=>import("../views/system/editPassword.vue"),
                meta: {
                    title: '密码修改',
                    icon: 'SwitchButton',
                }
            },
        ]
    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 全局守卫：登录拦截 本地没有存token,请重新登录
router.beforeEach((to, from, next) => {
    // 判断有没有登录
    // let name = "TOKEN" + "=";
    // let ca = document.cookie.split(';');
    // let token = "";
    // for (let i = 0; i < ca.length; i++) {
    //     let c = ca[i];
    //     while (c.charAt(0) == ' ') c = c.substring(1);
    //     if (c.indexOf(name) != -1) {
    //         token = c.substring(name.length, c.length);
    //     }
    // }
    //
    // if (token !== "") {
    //     console.log("获取到的:TOKEN:" + token)
    //     if (to.name == "login") {
    //         next();
    //     } else {
    //         router.push('/login')
    //     }
    // } else {
    //     next();
    // }
    console.log("路由守卫获取到的TOKEN:" + sessionStorage.getItem('TOKEN'))
    if (!sessionStorage.getItem('TOKEN')) {
        // @ts-ignore
        console.log(`to.name:${to.name}`);
        if (to.name == "login") {
            next();
        } else {
            router.push('/login')
        }
    } else {
        next();
    }
});
export default router
